define(function(require, exports, module) {
	"use strict";
	require("mui");
	require("jquery");
	var template = require("template");
	var abpUse = require("../abp.js");
	var abp = abpUse.abpstart;
	var muiTips = require("muiTips"); //封装的弹框
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//钉钉返回清除缓存
	var dingTalkNav = require('dd_config');
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authorityWhCheckDet = authorityInit.getJurisdiction(authorityData, sourceType.WH_CHECK); //采购记录权限判断

	function getRightFun() {
		var popoverMore = $("header > a").attr("href");
		mui(popoverMore).popover('toggle');
	}

	if(authorityWhCheckDet.indexOf(3) === -1) {
		muiTips.alertTips(' ', '您没有操作盘点记录的权限，请联系管理员开通吧！', 2, 3, "default");
		$("header > a").remove();
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(false, "更多", "default");
	} else {
		$("header > a").show();
		//钉钉右边按钮操作
		dingTalkNav.RightNavBar(true, "更多", getRightFun);
	}
	if(authorityWhCheckDet.indexOf(2) === -1) {
		$('#creatProfit').remove();
		$('#creatLoss').remove();
	} 
	mui("body").on("tap", "header>button", function() {
		sessionStorage.setItem('check_id', id);
		document.location.href = '../../src/inventoryRecord/index.html?dd_nav_bgcolor=FFFF943E';
	});

	function backList() {
		document.location.href = '../../src/inventoryRecord/index.html?dd_nav_bgcolor=FFFF943E';
	}
	//钉钉返回
	dingTalkNav.LeftNavBar(backList);
	//ad
	dingTalkNav.leftAndroid(backList);
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	var id = sessionStorage.getItem('check_id');
	var serialNo = "";
	var option = {};
	var dataModel = {
		"Id": id
	};
	var dataModel2 = {
		"id": id,
		"PageIndex": 1,
		"PageSize": 10
	};
	var profitValue = 0;
	var lossValue = 0;
	var orderDetail;
	var checkStatusId = 0;
	abp.services.app.checkList.getModel(dataModel, option)
		.done(function(data) {
			$("#whName").text(data.whName);
			$("#catalogName").text(data.catalogName);
			$("#serialDate").text(data.serialDate.substring(0, 10));
			$("#operatorName").text(data.operatorName);
			$("#statusName").text(data.statusName);
			serialNo = data.serialNo;
			var dataDetail = data;
			checkStatusId = dataDetail.statusId;
			abp.services.app.checkList.getDetailPage(dataModel2, option)
				.done(function(data) {
					$(".totalCount").text(data.totalCount);

					var items = data.items;
					$.each(items, function(i, v) {
						v.checkLossNumber = parseFloat(v.checkLossNumber);
						v.checkOverageNumber = parseFloat(v.checkOverageNumber);
						profitValue += v.checkOverageNumber;
						lossValue += v.checkLossNumber;
						if(v.checkNumber == -1) {
							v.checkNumber = 0;
						}
					});
					ButtonControl(dataDetail.statusId, dataDetail.isInput, dataDetail.isOutput, dataDetail.whInputList, dataDetail.whOutputList);
					$(".profit").text(parseFloat(profitValue));
					$(".loss").text(parseFloat(lossValue));
					var html = template("checkList", data);
					$("#check-goods-items").html(html);
				})
				.fail(function(data) {

				});

		})
		.fail(function(data) {

		});

	var ButtonControl = function(statusId, isInput, isOutput, WhInputList, WhOutputList) {
		if(statusId == abp.statusId.Draft) {
			$("#goCheck").show();
			$("#creatProfit").hide();
			$("#creatLoss").hide();
		} else {
			$("#goCheck").hide();
			$("#creatProfit").hide();
			$("#creatLoss").hide();
			if(isInput && isOutput) {
				$("#fromInput").show();
				$("#fromOutput").show();
				$("#inputSourceSerial").attr("data-id", WhInputList.id);
				$("#outputSourceSerial").attr("data-id", WhOutputList.id);
				$("#inputSourceSerial").text(WhInputList.serialNo);
				$("#outputSourceSerial").text(WhOutputList.serialNo);
			} else {
				if(profitValue > 0 && isInput === false) {
					$("#creatProfit").show();
				}
				if(lossValue > 0 && isOutput === false) {
					$("#creatLoss").show();
				}
				if(isInput) {
					$("#fromInput").show();
					$("#inputSourceSerial").attr("data-id", WhInputList.id);
					$("#inputSourceSerial").text(WhInputList.serialNo);
				}
				if(isOutput) {
					$("#fromOutput").show();
					$("#outputSourceSerial").attr("data-id", WhOutputList.id);
					$("#outputSourceSerial").text(WhOutputList.serialNo);
				}
			}
		}
	};
	mui("body").on("tap", "#goCheck", function() {
		sessionStorage.setItem('check_id', id);
		document.location.href = '../newCheckStock/checkInput.html?dd_nav_bgcolor=FFFF943E';
	});

	mui("body").on("tap", "#inputSourceSerial", function() {
		sessionStorage.setItem('SourceId', $(this).attr("data-id"));
		document.location.href = '../inStock/inStockDetail.html?dd_nav_bgcolor=FFFF943E';
	});
	mui("body").on("tap", "#outputSourceSerial", function() {
		sessionStorage.setItem('SourceId', $(this).attr("data-id"));
		document.location.href = '../outStock/outStockDetail.html?dd_nav_bgcolor=FFFF943E';
	});

	mui("body").on("tap", "#creatProfit", function() {
		//盘盈入库
		var dataModel = {
			"id": id
		};
		abp.services.app.checkList.createOverageInput(dataModel, option)
			.done(function(data) {
				//alert("生成盘盈单成功!");
				sessionStorage.setItem('SourceId', data.id);
				document.location.href = '../inStock/inStockDetail.html?dd_nav_bgcolor=FFFF943E';
				//          	dataModel.id=data.id;
				//          	abp.services.app.whInputList.getModel(dataModel,option).
				//		        done(function(data){		        	
				//		        	orderDetail=JSON.stringify(data);
				//		        	sessionStorage.orderDetail=orderDetail;
				//					sessionStorage.flag="change";
				//				    document.location.href="../newInstock/newInstockOrder.html";
				//		        }).fail(function(data){
				//		        	
				//		        });
			})
			.fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
	});
	mui("body").on("tap", "#creatLoss", function() {
		//盘亏出库
		var dataModel = {
			"id": id
		};
		abp.services.app.checkList.createLossOutput(dataModel, option)
			.done(function(data) {
				//alert("生成盘亏单成功!");   
				sessionStorage.setItem('SourceId', data.id);
				document.location.href = '../outStock/outStockDetail.html?dd_nav_bgcolor=FFFF943E';
				//          	dataModel.id=data.id;
				//          	abp.services.app.whInputList.getModel(dataModel,option).
				//		        done(function(data){		        	
				//		        	orderDetail=JSON.stringify(data);
				//				    sessionStorage.orderDetail=orderDetail;
				//					sessionStorage.flag="change";
				//				    document.location.href="../newOutstock/newOutstockOrder.html";
				//				    
				//		        }).fail(function(data){
				//		        	
				//		        });

			})
			.fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
	});

	function setDelete() {
		if(checkStatusId == abp.statusId.Finish || checkStatusId == abp.statusId.Referenced) {
			var muiTips = require("muiTips");
			muiTips.alertTips(' ', "单据已被引用不能删除！", 2, 2, "default");
		} else {
			dataModel.SerialNo = serialNo;
			abp.services.app.checkList.delete(dataModel, option)
				.done(function(data) {
					//								alert("删除成功");
					sessionStorage.removeItem("check_id");
					document.location.href = '../inventoryRecord/index.html?dd_nav_bgcolor=FFFF943E';
				})
				.fail(function(data) {

				});
		}
	}
	mui('body').on('tap', '.mui-popover-action li>a', function() {
		var a = this,
			parent;
		//根据点击按钮，反推当前是哪个actionsheet
		for(parent = a.parentNode; parent != document.body; parent = parent.parentNode) {
			if(parent.classList.contains('mui-popover-action')) {
				break;
			}
		}
		//关闭actionsheet
		mui('#' + parent.id).popover('toggle');
		if($(this).text() == "删除") {
			muiTips.confirmTips(' ', '您确定要删除吗！', 2, 1, setDelete);
		}
	});
});